﻿using System.Collections.Generic;
using Lari.Configuration;
using Lari.DataAccess;
using Lari.Framework.Utility;
using Lari.Services.Data;
using Lari.Services.ViewModel;

namespace Lari.Services.Implementation
{
    public class UserManagementManager : LariService
    {
        #region Roles
        public List<AppModule> GetAllModules()
        {
            List<AppModule> ret;
            using (var db = new LariDbContext(false))
            {
                ret = UserManagerDataAccess.GetAllModules(db);
                ret.ForEach(mod =>
                {
                    if (!ApplicationConfig.ConfiguredModules.Contains(mod.ID))
                    {
                        ret.Remove(mod);
                    }
                });
            }

            return ret;
        }

        public List<Role> GetAllRoles(string search)
        {
            using (var db = new LariDbContext(false))
            {
                return UserManagerDataAccess.GetAllRoles(search, db);
            }
        }

        public List<AppModule> GetAllModulesWithAccess()
        {

            using (var db = new LariDbContext(false))
            {
                var ret = UserManagerDataAccess.GetAllModulesWithAccess(db);
                // remove not purchage modules
                ret.ForEach(mod =>
                {
                    if (!ApplicationConfig.ConfiguredModules.Contains(mod.ID))
                        ret.Remove(mod);
                });
                return ret;

            }


        }

        public Role GetCompleteRole(int roleId)
        {
            using (var db = new LariDbContext(false))
            {
                return UserManagerDataAccess.GetCompleteRole(roleId, db);
            }
        }
        public Role RoleOnly(int roleid)
        {
            using (var db = new LariDbContext(false))
            {
                return UserManagerDataAccess.RoleOnly(roleid, db);
            }
        }
        public void SaveRole(RoleModel role)
        {
            using (var db = new LariDbContext(false))
            {
                UserManagerDataAccess.SaveRole(role, db);
            }
        } 
        #endregion

        #region Profile
        public bool ValidateUser(string userid, string currentPassword)
        {
            var temPassword = Encryption.Encrypt(currentPassword);
            using (var db = new LariDbContext(false))
            {
                return UserManagerDataAccess.ValidateUser(userid, temPassword, db);
            }
        } 
        
        public void ChangePassword(string userId, string newPassword)
        {
            using (var db = new LariDbContext(false))
            {
                 UserManagerDataAccess.ChangePassword(userId, newPassword, db);
            }
        }

        public void SaveHintQuestionAnswer(UserProfileModel model)
        {
            using (var db = new LariDbContext(false))
            {
                UserManagerDataAccess.SaveHintQuestionAnswer(model, db);
            }
        }

        #endregion

        public int SaveEmployee(EmployeeModel model)
        {
            using (var db = new LariDbContext(false))
            {
                return UserManagerDataAccess.SaveEmployee(model, db);
            }
        }

        public Employee Employee(int id)
        {
            using (var db = new LariDbContext(false))
            {
                return UserManagerDataAccess.Employee(id, db);
            }
        }

        public void SaveUser(int empId, string userid, string password,int roleid)
        {
            using (var db = new LariDbContext(false))
            {
                 UserManagerDataAccess.SaveUser(empId,userid,password,roleid, db);
            }
        }
        
        public void ChangeEnployeeActivStatus(int empId , bool status)
        {
            using (var db = new LariDbContext(false))
            {
                UserManagerDataAccess.ChangeEnployeeActivStatus(empId,status, db);
            }
        }

        public UserMgrGlanceModel GetAdminGlance()
        {
            using (var db = new LariDbContext(false))
            {
                return UserManagerDataAccess.GetAdminGlance(db);
            }
        }

        public bool IsCurrentUserHasHintQuestion()
        {
            using (var db = new LariDbContext(false))
            {
                return UserManagerDataAccess.IsCurrentUserHasHintQuestion(db);
            }
        }

        public List<EmployeesUsersView> SearchEmployeesUser(string searchParam,int currentPage,ref int totalPageCount)
        {
            using (var db = new LariDbContext(false))
            {
                return UserManagerDataAccess.SearchEmployeesUser(db, searchParam,currentPage ,ref totalPageCount);
            }
        }

        public void ChangeRole(int empId, int roleId)
        {
            using (var db = new LariDbContext(false))
            {
                UserManagerDataAccess.ChangeRole(db, empId, roleId);
            }
        }

        public List<Specialization> SepecializationForDepartment(int departmentId)
        {
            using (var db = new LariDbContext(false))
            {
                return UserManagerDataAccess.SepecializationForDepartment(db, departmentId);
            }
        }

        public List<ConsultantSpecialization> SpecializationForConsultant(int empId)
        {
            using (var db = new LariDbContext(false))
            {
                return UserManagerDataAccess.SpecializationForConsultant(db, empId);
            }
        }

        public void AddConsultantSpecialization(int empId, List<int> specializationList)
        {
            using (var db = new LariDbContext(false))
            {
                UserManagerDataAccess.AddConsultantSpecialization(db, empId, specializationList);
            }
        }

        public void RemoveConsultantSpecialization(int empId, List<int> specializationList)
        {
            using (var db = new LariDbContext(false))
            {
                UserManagerDataAccess.RemoveConsultantSpecialization(db, empId, specializationList);
            }
        }
    }
}
